var URL_Pay = "PY0304.web";// 订单支付

/**
 * 查询要支付的订单信息
 */
function getOrderInfo(){
	formSubmitForAjax({
		url : BASE_PATH + "PY0303.web",
		data : {"prdOrdNo" : prdOrdNo},
		success : function(json) {
			try {
				if (json["RSPCOD"] == "000000") {
					orderPay(json["orderInfo"]);
				}else{
					alert("操作失败,请重新支付！");
				}
			} catch (e) {
				alert("操作失败！");
			}
		}
	});
}

/**
 * 调用订单支付接口
 */
function orderPay(orderInfo) {
	var data = {};
	data["payeePhone"] = $.getCookie("_p");
	data["routeMerId"] = $.getCookie("MerNo");
	data["api_code"] = api_code;
	data["prdOrdNo"] = orderInfo['prdOrdNo'];
	data["payAmt"] = orderInfo['ordamt'] / 100;
	data["service"] = "order.pay";
	data["fn"] = "create_public";
	data["payType"] = "04";
	var url = BASE_PATH + URL_Pay;
	formSubmitForAjax({
		url : url,
		data : data,
		success : function(json) {
			try {
				if (json["RSPCOD"] == "000000") {
					var busContent = json['busContent'];
					if (!busContent) {
						alert('操作失败,请重新支付');
						return;
					}
					
					var token_id = busContent['token_id'];
					var pay_info = busContent['pay_info'];//二维码图片地址
					if(!token_id || !pay_info){
						alert('操作失败,请重新支付');
						return;
					}
					if(typeof pay_info === 'string'){
						pay_info = jQuery.parseJSON(pay_info);
					}
					pay_info['ordAmt'] = data["payAmt"];
//					for(var key in pay_info){
//						alert(key + "=" + pay_info[key]);
//					}
					setTimeout(function(){weixinPay(pay_info);}, 500);
					
				} else {
					alert(json.RSPMSG);
				}
			} catch (e) {
				alert("操作失败!");
			}
		}
	});
}

function weixinPay(pay_info){
	WeixinJSBridge.invoke('getBrandWCPayRequest',{
		"appId" : pay_info['appId'] + "", //公众号名称，由商户传入
		"timeStamp":pay_info['timeStamp'] + "", //时间戳，自 1970 年以来的秒数
		"nonceStr" : pay_info['nonceStr'] + "", //随机串
		"package" : pay_info['package'] + "",
		"signType" : pay_info['signType'] + "", //微信签名方式:
		"paySign" : pay_info['paySign'] + "" //微信签名
		},function(res){
		if(res.err_msg == "get_brand_wcpay_request:ok" ) {//成功
			//跳转到成功页面
			var callback_url = pay_info['callback_url'] + "?ordAmt=" + pay_info['ordAmt'];
			open(callback_url);
		}else if(res.err_msg == "get_brand_wcpay_request:cancel"){//取消
			
		}else{
			alert("支付失败,请重新支付,如有疑问,请与商家核对支付结果!");
		}
	});
//	WeixinJSBridge.invoke('getBrandWCPayRequest',{
//		"appId" : "wx2421b1c4370ec43b", //公众号名称，由商户传入
//		"timeStamp":" 1395712654", //时间戳，自 1970 年以来的秒数
//		"nonceStr" : "e61463f8efa94090b1f366cccfbbb444", //随机串
//		"package" : "prepay_id=u802345jgfjsdfgsdg888",
//		"signType" : "MD5", //微信签名方式:
//		"paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名
//	},function(res){
//		if(res.err_msg == "get_brand_wcpay_request:ok" ) {}
//		// 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg 将在用户支付成功后返回 ok，但并不保证它绝对可靠。
//	});
}

//加载完成执行
$(document).ready(function() {
	getOrderInfo();
});

